﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models;

namespace GameSchool.DAL
{
    public class ProjectRepository  : IProjectRepository, IDisposable
    {
        // Nánari upplýsingar um hvernig Repository patternið virkar er á:
        // http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

        /// <summary>
        /// Meðlimabreyta sem er gagnagrunnstengingin
        /// </summary>
        private gsEntities m_db;

        /// <summary>
        /// Class Initializer sem tekur við gagnagrunnstengingu sem skal nota
        /// </summary>
        /// <param name="db"></param>
        public ProjectRepository(gsEntities db)
        {
            this.m_db = db;
        }

        /// <summary>
        /// Skilar lista yfir öll prófin
        /// </summary>
        /// <returns></returns>
        public List<Project> GetProject()
        {
            return m_db.Project.ToList();
        }

        /// <summary>
        /// Sækir eitt próf eftir ID
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public Project GetProjectById(int projectId)
        {
            return m_db.Project.SingleOrDefault(e => e.ProjectID == projectId);
        }

        public List<ProjectReturns> GetProjectReturnsByProject(int projectId)
        {
            return m_db.ProjectReturns.Where(i => i.ProjectID == projectId).ToList();
        }

        /// <summary>
        /// Bætir við prófi í gagnagrunninn. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="Project"></param>
        public void InsertProject(Project project)
        {
            m_db.Project.AddObject(project);
        }

        public ProjectReturns GetProjectReturnsByProjectReturnId(int projectReturnId)
        {
            return m_db.ProjectReturns.SingleOrDefault(i => i.ProjectReturnID == projectReturnId);
        }

        public void InsertProjectReturns(ProjectReturns projectReturn)
        {
            m_db.ProjectReturns.AddObject(projectReturn);
        }

        /// <summary>
        /// Eyðir prófi úr gagnagrunninum. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="projectId"></param>
        public void DeleteProject(int projectId)
        {
            // Við skulum ekkert gera sérstakt hérna þar sem við viljum ekki leyfa að eyða prófum.
            // Annars væri kóðinn fyrir það svona:

            //var toDelete = m_db.Project.SingleOrDefault(e => e.ProjectID == projectId);
            //m_db.Project.DeleteObject(toDelete);
            //m_db.SaveChanges();
        }

        /// <summary>
        /// Uppfærir próf í gagnagrunninum. ATH: Vistar ekki sjálfkrafa
        /// </summary>
        /// <param name="project"></param>
        public void UpdateProject(Project project)
        {
            m_db.Project.Attach(project);
        }

        public Course GetCourseById(int courseId)
        {
            return m_db.Course.SingleOrDefault(i => i.CourseID == courseId);
        }
        /// <summary>
        /// Vistar breytingar sem gerðar voru í sessioninu
        /// </summary>
        public void Save()
        {
            m_db.SaveChanges();
        }

        public void InsertProjectWithGrade(ProjectReturns projectReturn)
        {
            m_db.ProjectReturns.AddObject(projectReturn);
        }


        public List<vProjectsTeacher> GetProjectsTeacher(int courseId, Guid teacherUserId)
        {

            return m_db.vProjectsTeacher
                .Where(i => i.CourseID == courseId && i.TeacherUserID == teacherUserId)
                .OrderBy(i => i.Name)
                .ToList();
            //return m_db.vProjectsTeacher.OrderBy(o => o.Name).ToList();
        }

        public List<vProjectsStudents> GetProjectsStudents(int courseId)
        {
            return m_db.vProjectsStudents
            .Where(i => i.CourseID == courseId)
            .OrderBy(i => i.Name)
            .ToList();

        }

        public List<vProjectsStudents> GetProjectsStudents(int courseId, Guid studentUserId)
        {
            return m_db.vProjectsStudents
            .Where (i => i.CourseID == courseId && i.StudentUserID == studentUserId)
            .OrderBy(i => i.Name)
            .ToList();

        }

        #region Disposerinn fyrir using() statement - Alltaf eins
        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    m_db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}